package com.xuecheng.content.service.impl;

import com.alibaba.fastjson.JSON;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.model.dto.*;
import com.xuecheng.content.model.po.*;
import com.xuecheng.content.mapper.CoursePublishMapper;
import com.xuecheng.content.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程发布 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class CoursePublishServiceImpl extends ServiceImpl<CoursePublishMapper, CoursePublish> implements CoursePublishService {

    @Autowired
    CourseBaseService courseBaseService;

    @Autowired
    CourseMarketService courseMarketService;

    @Autowired
    CourseTeacherService courseTeacherService;

    @Autowired
    TeachplanMediaService teachplanMediaService;

    @Autowired
    CoursePublishPreService coursePublishPreService;

    @Autowired
    CoursePublishMapper coursePublishMapper;

    @Autowired
    TeachplanService teachplanService;
    @Override
    public CourseInfoDto selectCourseBaseInfo(String courseId) {
        CourseBase courseBase = courseBaseService.selectById(Long.valueOf(courseId));
        CourseMarket courseMarket = courseMarketService.selectById(courseId);
        CourseTeacher courseTeacher = courseTeacherService.selectByCourseId(courseId);
        List<TeachplanTreeNodeDto> teachplanTreeNodeDtos = teachplanService.selectTeachTreeNodes(Long.valueOf(courseId));

        return new CourseInfoDto(courseBase,courseMarket,courseTeacher,teachplanTreeNodeDtos);
    }

    @Override
    public CoursePreviewDto getCoursePreviewInfo(Long courseId){

        CourseBaseInfoDto courseBaseInfo = courseBaseService.selectCourseBaseAndMarketById(courseId);
        List<TeachplanTreeNodeDto> teachplanTreeNodeDtos = teachplanService.selectTeachTreeNodes(courseId);
        if (courseBaseInfo==null && teachplanTreeNodeDtos==null){
            XueChengPlusException.cast("课程信息不存在");
        }
        return new CoursePreviewDto(courseBaseInfo,teachplanTreeNodeDtos);
    }

    //提交
    @Override
    public void commitAudit(Long courseId, Long companyId) {
//        校验   课程的审核状态为已提交则不允许提交
        CourseBaseInfoDto courseBaseInfo = courseBaseService.selectCourseBaseAndMarketById(courseId);
        if(courseBaseInfo == null){
            XueChengPlusException.cast("课程信息不存在");
        }
        if(courseBaseInfo.getAuditStatus().equals("202003")){
            XueChengPlusException.cast("课程已提交，请勿重复提交");
        }
        if(courseBaseInfo.getPic().isEmpty()){
            XueChengPlusException.cast("课程图片不能空");
        }
        List<TeachplanTreeNodeDto> teachplanTreeNodeDtos = teachplanService.selectTeachTreeNodes(courseId);
        if(teachplanTreeNodeDtos == null || teachplanTreeNodeDtos.size() == 0){
            XueChengPlusException.cast("课程计划不能为空");
        }

        //        查询 课程基本信息 营销信息 计划信息 插入到课程预发布表
        CoursePublishPre coursePublishPre = new CoursePublishPre();
        coursePublishPre.setCompanyId(companyId);       //设置机构id
        BeanUtils.copyProperties(courseBaseInfo,coursePublishPre);
        CourseMarket courseMarket = courseMarketService.selectById(String.valueOf(courseId));      //营销信息转JSON
        String courseMarketJson = JSON.toJSONString(courseMarket);
        coursePublishPre.setMarket(courseMarketJson);
        String teachplanJson = JSON.toJSONString(teachplanTreeNodeDtos);  //课程计划转JSON
        coursePublishPre.setTeachplan(teachplanJson);
        coursePublishPre.setStatus("202003");               //修改状态
        coursePublishPre.setCreateDate(LocalDateTime.now()); //设置创建时间
        coursePublishPre.setStName(courseBaseInfo.getSt());
        coursePublishPre.setMtName(courseBaseInfo.getMt());

//        查询课程预发布表 存在则更新，不存在则插入
        CoursePublishPre coursePublishPre1 = coursePublishPreService.selectById(courseId);
        if(coursePublishPre1 == null){
            coursePublishPreService.insert(coursePublishPre);
        }else{
            coursePublishPreService.updateById(coursePublishPre);
        }

//        更新课程基本信息表的审核状态为已提交
        CourseBase courseBase = courseBaseService.selectById(courseId);
        courseBase.setAuditStatus("202003");
        courseBaseService.updateById(courseBase);
    }

    @Override
    public void publish(Long courseId, long l) {
        //        查询预发布表
        CoursePublishPre coursePublishPre = coursePublishPreService.selectById(courseId);
        if(coursePublishPre == null){
            XueChengPlusException.cast("课程没有提交审核");
        }
        if(coursePublishPre.getStatus().equals("202003")){
            XueChengPlusException.cast("课程未审核");
        }
//        课程预发布表写数据
        CoursePublish coursePublish = new CoursePublish();
        BeanUtils.copyProperties(coursePublishPre,coursePublish);
        CoursePublish selectCoursePublish = coursePublishMapper.selectById(courseId);
        if(selectCoursePublish == null){
            coursePublishMapper.insert(coursePublish);
        }else{
            coursePublishMapper.updateById(coursePublish);
        }
//        消息表写数据
//        saveCoursePublishMessage(courseId);
//        预发布表数据删除
        coursePublishPreService.deleteById(courseId);
    }

    @Override
    public CoursePublish getCoursePublish(Long courseId) {
        return coursePublishMapper.selectById(courseId);
    }

}































